package org.jboss.seam.example.jpa;

import static org.jboss.seam.ScopeType.SESSION;

import java.util.List;

import javax.persistence.EntityManager;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;

import br.com.rafaelSys.entidades.Usuario;

@Name("authenticator")
public class AuthenticatorAction
{
	@In
	EntityManager entityManager;

	@Out(required=false, scope = SESSION)
	private Usuario user;

	@SuppressWarnings("unchecked")
	public boolean authenticate()
	{
		System.out.println("Authentication");
		List results = entityManager.createQuery("select u from Usuario u where u.username=#{identity.username} and u.password=#{identity.password}")
		.getResultList();

		if ( results.size()==0 )
		{
			return false;
		}
		else
		{
			user = (Usuario) results.get(0);
			System.out.println("Authentication ok : " + user.getName());
			return true;
		}
	}

}
